unit clsIcmsDiferido;

interface

uses dm, dbtables, biblio, classes;

type
  TIcmsDiferido = class(TObject)
  public
    Icms_Descricao: string;
    Icms_Percentual: double;
    function Recuperar(Icms_Codigo: string): Boolean;
    function Recuperar_Percentual(p_Percentual: double): string;
    function Ultimo: string;
    procedure Gravar(Icms_Codigo: string);
    procedure Excluir(Icms_Codigo: string);
    procedure Consultar(p_Descricao: string; var tq: TQuery);
    procedure Listar_Icms(var lista: TStringList);
    function Existe: Boolean;
  end;

implementation

procedure TIcmsDiferido.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from IcmsDiferido where Icms_Codigo=:a');
  tq.params[0].asstring := Icms_Codigo;
  tq.open;
  if tq.eof then begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into IcmsDiferido values(' +
      ':k_Icms_Codigo    ,' +
      ':k_Icms_Descricao ,' +
      ':k_Icms_Percentual)');
  end else begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update IcmsDiferido set            ' +
      'Icms_Descricao =:k_Icms_Descricao ,' +
      'Icms_Percentual=:k_Icms_Percentual ' +
      'where Icms_Codigo=:k_Icms_Codigo   ');
  end;
  tq.parambyname('k_Icms_Codigo').asstring := Icms_Codigo;
  tq.parambyname('k_Icms_Descricao').asstring := Icms_Descricao;
  tq.parambyname('k_Icms_Percentual').asfloat := Icms_Percentual;
  tq.execsql;
  tq.close;
  tq.free;
end;

function TIcmsDiferido.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from IcmsDiferido where Icms_Codigo=:a');
  tq.params[0].asstring := Icms_Codigo;
  tq.open;
  Icms_Descricao := tq.fieldbyname('Icms_Descricao').asstring;
  Icms_Percentual := tq.fieldbyname('Icms_Percentual').asfloat;
  result := not tq.eof;
  tq.close;
  tq.free;
end;

procedure TIcmsDiferido.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from IcmsDiferido where Icms_Codigo=:a');
  tq.params[0].asstring := Icms_Codigo;
  tq.execsql;
  tq.close;
  tq.free;
end;

procedure TIcmsDiferido.Consultar;
begin
  tq.close;
  tq.sql.Clear;
  tq.params.Clear;
  tq.sql.add('Select * from IcmsDiferido where Icms_Descricao>=:a order by Icms_Descricao');
  tq.params[0].asstring := p_Descricao;
  tq.open;
end;

function TIcmsDiferido.Ultimo;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select max(Icms_Codigo) as ultimo from IcmsDiferido');
  tq.open;
  result := strzero(tq.fieldbyname('ultimo').asinteger + 1, 2);
  tq.close;
  tq.free;
end;

procedure TIcmsDiferido.Listar_Icms;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.sql.add('Select * from IcmsDiferido order by icms_codigo');
  tq.open;
  lista.clear;
  while not tq.eof do begin
    lista.add(tq.fieldbyname('Icms_Codigo').asstring + '-' + tq.fieldbyname('Icms_Descricao').asstring + ' (' + tq.fieldbyname('Icms_Percentual').asstring + '%)');
    tq.next;
  end;
  tq.close;
  tq.free;
end;

function TIcmsDiferido.Recuperar_Percentual;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from IcmsDiferido where Icms_Percentual=:a');
  tq.params[0].asfloat := p_Percentual;
  tq.open;
  if tq.fieldbyname('Icms_Codigo').asstring = '' then result := '01' else result := tq.fieldbyname('Icms_Codigo').asstring;
  tq.close;
  tq.free;
end;

function TIcmsDiferido.Existe: Boolean;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from IcmsDiferido');
  tq.open;
  result := not tq.eof;
  tq.close;
  tq.free;

end;

end.

